package com.ruoyi.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.AnonymousAccess;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.vo.Result;
import com.ruoyi.system.domain.PaymentRecord;
import com.ruoyi.system.domain.ProductKey;
import com.ruoyi.system.domain.vo.ClientProductKeyListRes;
import com.ruoyi.system.domain.vo.ClientProductKeyListVo;
import com.ruoyi.system.service.IPaymentRecordService;
import com.ruoyi.system.service.IProductKeySevice;
import com.ruoyi.system.service.IVerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("/system/productKey")
@Api(value = "客户端卡密")
public class ProductKeyController {

    @Autowired
    private IProductKeySevice productKeySevice;

    @Autowired
    private IPaymentRecordService paymentRecordService;

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/list")
    @ApiOperation(value="客户端卡密-查询订单", notes="支付-查询订单")
    @AnonymousAccess
    public Result<?> list(ClientProductKeyListVo clientProductKeyListVo,
                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                          @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                          HttpServletRequest request) {
        try {
            String email = verifyCodeService.checkClientToken(request);
            // 如果未传入邮箱，则在邮箱验证后从redis中获取
            if (ObjectUtil.notEqual(email, clientProductKeyListVo.getBuyer())) {
                return Result.noauth("当前邮箱与认证邮箱不符");
            }
        } catch (Exception e) {
            Result.noauth(e.getMessage());
        }
        //todo 需要校验邮箱或者手机号
        Optional.ofNullable(StrUtil.emptyToNull(clientProductKeyListVo.getBuyer())).orElseThrow(() -> new ServiceException("请输入购买信息"));
        Page<ClientProductKeyListVo> page = new Page<>(pageNo, pageSize);
        List<ClientProductKeyListRes> res =  productKeySevice.queryClientProductKey(page, clientProductKeyListVo);
        return Result.OK(res);
    }

    @GetMapping("/queryStatus")
    @ApiOperation(value="客户端卡密-查询订单-状态", notes="查询订单-状态")
    @AnonymousAccess
    public Result<?> queryStatus(@RequestParam(name="id") Long id) {
        ClientProductKeyListVo clientProductKeyListVo = new ClientProductKeyListVo();
        clientProductKeyListVo.setId(id);
        ClientProductKeyListRes res = productKeySevice.queryClientProductKey(clientProductKeyListVo);
        Optional.ofNullable(res).orElseThrow(() -> new ServiceException("不存在的订单"));
        return Result.OK(res);
    }
}
